package com.diaohw.module.file.service.impl;

import cn.hutool.core.io.FileUtil;
import com.diaohw.module.file.mapper.FileMapper;
import com.diaohw.module.file.obj.entity.FileDO;
import com.diaohw.module.file.obj.query.FileQuery;
import com.diaohw.module.file.obj.tables.FileDef;
import com.diaohw.module.file.obj.vo.FileVO;
import com.diaohw.module.file.service.FileService;
import com.diaohw.module.file.utils.MinioUtil;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.common.util.CryptoUtil;
import com.diaohw.platform.framework.web.util.RequestUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 文件信息 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-09-20
 */
@Service
public class FileImpl implements FileService {
    @Resource
    private FileMapper fileMapper;

    @Override
    public Boolean delete(String id) {
        return fileMapper.deleteById(id) > 0;
    }

    @Override
    public FileVO info(String id) {
        FileDO model = fileMapper.selectOneById(id);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public void detail(String id) {
        FileDO fileInfo = fileMapper.selectOneById(id);

        InputStream download = MinioUtil.download(fileInfo.getBucket(), fileInfo.getPath());
        assert download != null;
        RequestUtil.download(download, fileInfo.getContentType(), fileInfo.getFilename(), fileInfo.getLength());
    }

    @Override
    public List<FileVO> findByList(FileQuery query) {
        QueryWrapper wrapper = FileMapper.createWrapper(query);

        List<FileDO> list = fileMapper.selectListByQuery(wrapper);
        return INSTANCE.entityToVo(list);
    }


    @Override
    public PageResultVo<FileVO> findByPage(FileQuery query) {
        QueryWrapper wrapper = FileMapper.createWrapper(query);
        Page<FileDO> page = fileMapper.paginate(query.getPageIndex(), query.getPageSize(), wrapper);
        List<FileVO> list = INSTANCE.entityToVo(page.getRecords());
        return PageResultVo.create(page.getTotalRow(), list);
    }


    @Override
    public FileVO upload(String bucket, MultipartFile file) {
        try {
            String md5 = CryptoUtil.md5(file.getInputStream());
            QueryWrapper wrapper = FileMapper.createWrapper(new FileQuery().setMd5(md5));

            FileDO info = fileMapper.selectOneByQuery(wrapper);

            if (info != null) {
                FileVO vo = INSTANCE.entityToVo(info);
                vo.setFilename(file.getOriginalFilename());
                return vo;
            }

            info = new FileDO();
            info.setMd5(md5)
                    .setBucket(bucket)
                    .setContentType(file.getContentType())
                    .setFilename(file.getOriginalFilename())
                    .setLength(file.getSize())
                    .setExt(FileUtil.extName(file.getOriginalFilename()).toLowerCase());
            MinioUtil.upload(file.getInputStream(), info);

            fileMapper.insert(info);
            return INSTANCE.entityToVo(info);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<String> ext() {
        QueryWrapper wrapper = QueryWrapper.create();
        wrapper.select(FileDef.FILE.EXT)
                .groupBy(FileDef.FILE.EXT);
        return fileMapper.selectListByQueryAs(wrapper, String.class);
    }
}
